from flask import Blueprint
from flask import redirect
from flask import request
from flask import session
from flask import url_for

admin_blu = Blueprint('admin', __name__, url_prefix='/admin')

from . import views


@admin_blu.before_request
def check_admin():
    # 从缓存中获取当前缓存的is_admin
    is_admin = session.get('is_admin', False)
    # 如果缓存中的用户不是管理员而且访问的网页不是管理员登录页面，则跳转到项目首页
    if not is_admin and not request.url.endswith(url_for('admin.login')):
        return redirect('/')
